package com.zat.mes.andrmes.dofun.work

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.BadFlatItem
import com.zat.mes.andrmes.data.GetPackBean
import com.zat.mes.andrmes.data.Itemm
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.dofun.work.adapter.SelectBadFlatAdapter
import com.zat.mes.andrmes.dofun.work.adapter.SelectScheduAdapter
import com.zat.mes.andrmes.dofun.work.vm.YarnInModel
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.PreferencesUtil
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_select_schedu.*
import kotlinx.android.synthetic.main.layout_title.*

class SelectBadFlatActivity : BaseActivity() {

    var workShopId:String?=null
    var yarnModel:YarnInModel?=null
    var currentPage: Int = 1;
    var selectBadFlatAdapter: SelectBadFlatAdapter?=null;
    override fun init() {
        initView()
        initData()
    }

    private fun initData() {
        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        workShopId = loginResultBean.workShopid

        rvSchedu.layoutManager = LinearLayoutManager(baseContext)
        selectBadFlatAdapter = SelectBadFlatAdapter(R.layout.item_select_schedu)
        rvSchedu.adapter = selectBadFlatAdapter
        val loadMoreModule = selectBadFlatAdapter?.loadMoreModule
        // 当数据不满一页时，是否继续自动加载（默认为true）
        loadMoreModule?.isEnableLoadMoreIfNotFullPage = false
        loadMoreModule?.isAutoLoadMore = false
        loadMoreModule?.setOnLoadMoreListener(OnLoadMoreListener {
            requestData(workShopId)
        })
        yarnModel = ViewModelProvider(this).get(YarnInModel::class.java)

        yarnModel?.getBadFlatList()?.observe(this, Observer {
            if (it != null) {
                val result = it.result
                if (result != null) {
                    if (result.items != null) {
                        if (currentPage == 1) {
                            if (result.items == null || result.items.size <= 0) {
                                setEmptyVisible()
                            } else {
                                setEmptyGone()
                            }
                            selectBadFlatAdapter?.setNewData(result.items as MutableList<BadFlatItem>)
                        } else {
                            selectBadFlatAdapter?.addData(result.items)
                        }
                    }
                    if (result.currentPage == result.totalPage) {
                        loadMoreModule?.loadMoreEnd()
                    } else {
                        loadMoreModule?.loadMoreComplete()
                        currentPage++
                    }
                } else {
                    loadMoreModule?.loadMoreEnd()
                }
            } else {
                loadMoreModule?.loadMoreEnd()
            }
        })
        requestData(workShopId)
    }

    private fun requestData(workShopId: String?) {
        yarnModel?.getBadFlatList(currentPage,20,workShopId)

    }

    private fun initView() {
        setContentView(R.layout.activity_select_schedu)
        tVtitle.text = "选择废片"
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
        btnOkSelectSchedu.setOnClickListener {
            if (selectBadFlatAdapter?.selectPosition!=-1){
                val item = selectBadFlatAdapter?.getItem(selectBadFlatAdapter?.selectPosition!!)
                var data = Intent()
                data.putExtra("badFlat",item);
                setResult(Activity.RESULT_OK, data)
                finish()
            }else{
                showToast("请选择废片")
            }
        }
    }
}
